var App = (function(App, $, _){

	'user strict';

	var game = {};
	var moduleName = "game"; // define a unique module name

	game.parseMessage = function(message) {

		var msg = JSON.parse(message.message);

		console.log(msg);
		console.log(msg.event);

		if(msg.hasOwnProperty("event")) {
			
			// clean
			if(msg.event === "gameStarted") {

				App.trigger("gameStarted", {
					gameStatus: msg.data.gameStatus
				});

			}

			// reveal
			if(msg.event === "gameFinished") {

				App.trigger("gameFinished", {
					gameStatus: msg.data.gameStatus
				});
			}

			// interact
			if(msg.event === "gameUpdated") {

				App.trigger("gameUpdated", {
					gameStatus: msg.data.gameStatus
				});
				
			}

		}

	};

	game.updateGame = function(participantStatus) {

		App.log("GAME updateGame", participantStatus);		

		gapi.hangout.data.sendMessage(JSON.stringify({
			event: "gameParticipantStatus",
			data: {
				participantStatus: participantStatus
			}
		}));		

	};

	game.init = function() {

		// handle communication coming in
		gapi.hangout.data.onMessageReceived.add(game.parseMessage);

		// handle communication coming out
		App.on("gameParticipantStatus", game.updateGame);

	};

	// add module to list of modules
	App.modules = App.hasOwnProperty("modules") ? App.modules : {};
	App.modules[moduleName] = game;

	return App;

}(App || {}, $, _));